import request from '../utils/request/request'

export const state = () => ({
  user: null
})

export const mutations = {
  SET_USER(state, data) {
    Object.entries(data).forEach(([key, value]) => {
      state[key] = value
    })
  }
}

export const actions = {
  async login({ commit }, user = {}) {
    commit('SET_USER', { user })
    alert('登录成功')
  },

  async logout({ commit }) {
    await request({
      url: `/api/logout`
    })
    commit('SET_USER', { user: null })
  }
}
